package com.example.ythhome.login.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ythhome.login.entity.*;
import com.example.ythhome.login.service.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("api/userRole")
public class UserRoleController {

    @Autowired
    UserRoleService userRoleService;

    @PostMapping("/save")
    @ResponseBody
    public ResponseData<Boolean> save(@RequestBody Map<String, Object> params) {
        Integer userId = (Integer) params.get("userId");
        List<UserRole> userRoles = (List<UserRole>) params.get("userRoles");
        QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_Id", userId);
        userRoleService.remove(queryWrapper);
        return new ResponseData<Boolean>(ResponseData.StatusCodeEnum_SUCCESS, userRoleService.saveBatch(userRoles));
    }
    @PostMapping("/queryByUserId")
    @ResponseBody
    public ResponseData<List> queryByUserId(@RequestBody Integer userId){
        QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_Id", userId);
        return  new ResponseData<List>(ResponseData.StatusCodeEnum_SUCCESS,userRoleService.list(queryWrapper));
    };
}
